package com.jinyi.wwdeng.ui.steps.impl

import android.content.Context
import com.jinyi.base.mvp.BasePresenter
import com.jinyi.wwdeng.AppConfigs
import com.jinyi.wwdeng.ui.steps.contract.StepAccountContract
import org.json.JSONObject

/**
 * JinYiTest
 *
 * @Author：wwdeng
 * @CreateTime：2024/4/26 18:45
 * <pre>
 * Presenter 实现类
</pre> *
 */
class StepAccountPresenterImpl : BasePresenter<StepAccountContract.View?, StepAccountModelImpl?>(),
    StepAccountContract.Presenter {

    override fun createModel(): StepAccountModelImpl {
        return StepAccountModelImpl(this)
    }

    /**
     * 获取已绑定的账户列表
     */
    fun loadBindAccountListAction() {
        if (isViewDestory()) {
            return
        }
        model!!.loadBindAccountListAction()
    }

    /**
     * 添加绑定账户
     */
    fun addBindAccountAction(mobile: String, walletChannel: String, bindFlag:String = "NO") {
        if (isViewDestory()) {
            return
        }
        mvpView!!.showLoading("")
        model!!.addBindAccountAction(mobile, walletChannel, bindFlag)
    }

    /**
     * 删除账户
     */
    fun deleteAccountAction(info: JSONObject) {
        if (isViewDestory()) {
            return
        }
        mvpView!!.showLoading("")
        model!!.deleteAccountAction(info)
    }

    /**
     * 获取已绑定的账户列表成功
     */
    override fun onBindAccountListSuccess(data: JSONObject) {
        if (isViewDestory()) {
            return
        }
        when (data.optInt("code")) {
            0 -> {//成功
                val accountList = arrayListOf<JSONObject>()
                val array = data.optJSONArray("obj")

                if (array != null && array.length() > 0) {
                    for (i in 0..<array.length()) {
                        val item = array.optJSONObject(i)
                        item.put("isEdit", false)
                        accountList.add(item)
                    }
                }

                if (accountList.size < 2) {
                    for (i in 0..<2 - accountList.size) {
                        val item = JSONObject()
                        item.put("isEdit", true)
                        accountList.add(item)
                    }
                }
                mvpView!!.setBindAccountList(accountList)
            }

            else -> {//失败
                mvpView!!.showToast(data.optString("msg"))
            }
        }
    }

    /**
     * 添加绑定账户成功
     */
    override fun addBindAccountSuccess(mobile: String, data: JSONObject) {
        if (isViewDestory()) {
            return
        }
        when (data.optInt("code")) {
            0 -> {//成功
                //刷新绑定的账户列表
                mvpView!!.showLoading("")
                model!!.loadBindAccountListAction()

                val obj = data.optJSONObject("obj") ?: JSONObject()

                // "YES/NO 是否需要提示用户这个号码可以绑另一个渠道的钱包"
                val addBindFlag = obj.optString("addBindFlag")
                if (addBindFlag == "YES") {
                    //需要提示绑定另一个渠道账户
                    var otherAccountInfo: JSONObject? = null

                    //用于从APP配置中获取（walletChannels）对应的信息
                    val channel = obj.optString("channel")
                    if (channel.isNotBlank()) {
                        val spData = AppConfigs.getSPString(AppConfigs.SP_KEY.SP_HENECREDIT_COMMON)
                        val commonData = JSONObject(spData)
                        val customize = commonData.optJSONObject("customize")
                        if (customize != null) {
                            val walletChannels = customize.optJSONArray("walletChannels")
                            if (walletChannels != null && walletChannels.length() > 0) {
                                for (i in 0..<walletChannels.length()) {
                                    val item = walletChannels.optJSONObject(i)
                                    if (channel == item.optString("value")) {
                                        otherAccountInfo = item
                                        break
                                    }
                                }
                            }

                            // 只有非空的情况下才处理
                            if (otherAccountInfo != null) {
                                otherAccountInfo.put("walletMobile", mobile)
                                //提示绑定另一个渠道账户
                                mvpView!!.showBingAccountOtherDialog(otherAccountInfo)
                                return
                            }
                        }
                    }
                }
            }

            else -> {//失败
                mvpView!!.showToast(data.optString("msg"))
            }
        }
    }

    /**
     * 删除已绑定的账户成功
     */
    override fun onDeleteAccountSuccess(data: JSONObject) {
        if (isViewDestory()) {
            return
        }
        mvpView!!.showToast(data.optString("msg"))
        //获取已绑定的账户列表
        loadBindAccountListAction()
    }

    override fun onRequestError(errorMsg: String) {
        super.onRequestError(errorMsg)
        mvpView!!.showToast(errorMsg)
    }

    override fun onRequestFinish() {
        super.onRequestFinish()
        mvpView!!.hideLoading()
        mvpView!!.onRequestFinish()
    }

    override val context: Context?
        get() = if (!isViewDestory()) {
            mvpView!!.getContext()
        } else null
}
